Skip to content

fix: instrument MCP List Tools spans in OpenAI Agents#4387

Open
hashwnath wants to merge 1 commit intoopen-telemetry:mainfrom
hashwnath:fix/4197-mcp-list-tools-spans
Open

fix: instrument MCP List Tools spans in OpenAI Agents#4387
hashwnath wants to merge 1 commit intoopen-telemetry:mainfrom
hashwnath:fix/4197-mcp-list-tools-spans

Conversation

@hashwnath
Copy link
Copy Markdown

Description

Fixes #4197

Problem: The OpenAI Agents SDK emits MCPListToolsSpanData spans when an agent fetches the list of tools from an MCP server, but the instrumentation did not handle this span data type. As a result, these spans appeared as "unknown" with operation_name="unknown" and no useful attributes.

Fix:

  1. Import MCPListToolsSpanData in both the direct and fallback import blocks
  2. Operation name: Maps to "mcp.list_tools"
  3. Span kind: SpanKind.CLIENT (outgoing call to MCP server)
  4. Span naming: Uses mcp.list_tools {server_name} format (e.g., mcp.list_tools Time)
  5. Attributes: Extracts mcp.server.name, mcp.tools.count, and mcp.tools.list (list only when content capture is enabled)

Testing: Added unit test verifying operation name, span name, span kind, server attribute, and tools count for MCP list tools spans. All 104 existing tests continue to pass.

Note: A previous PR (#4285) attempted to fix this issue but was closed without merging. This PR takes a similar approach but integrates more completely with the existing patterns in the codebase, including proper span naming via get_span_name(), output type inference, and attribute extraction through the standard _extract_genai_attributes dispatch.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Handle MCPListToolsSpanData so that MCP list_tools spans are properly
instrumented with operation name "mcp.list_tools", SpanKind.CLIENT,
and MCP-specific attributes (server name, tools count, tools list)
instead of appearing as "unknown" spans with no data.

Fixes open-telemetry#4197
@hashwnath hashwnath requested a review from a team as a code owner April 2, 2026 08:25
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 2, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: hashwnath / name: Hashwanth S (49f3000)

@xrmx xrmx added the gen-ai Related to generative AI label Apr 2, 2026
@xrmx
Copy link
Copy Markdown
Contributor

xrmx commented Apr 2, 2026

@hashwnath thanks for the pr, in order to contribute to this project you need to sign the CLA

Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @hashwnath!

The implementation follows the existing patterns well. However, a concern before approving is that a number of attributes don't align with the MCP semantic conventions that are already in the Python semconv package (opentelemetry/semconv/_incubating/attributes/mcp_attributes.py).

mcp.server.name is not a semconv attribute

The registered MCP attributes are mcp.method.name, mcp.protocol.version, mcp.resource.uri, and mcp.session.id. There is no mcp.server.name. The MCP span spec uses server.address for server identity.

mcp.tools.count and mcp.tools.list are not in semconv

These are custom attributes with no semconv backing. We should not add non-spec attributes. You're welcome to open a semconv proposal to suggest them if you think they'd be valuable.

Span name and operation name should follow MCP semconv

Per the MCP span spec, span names should follow {mcp.method.name} {target}. For this span type the method name is tools/list (McpMethodNameValues.TOOLS_LIST), so the span name should be tools/list Time rather than mcp.list_tools Time. The mcp.method.name attribute should also be set on the span.

The GenAIOperationName.MCP_LIST_TOOLS = "mcp.list_tools" value also diverges from the rest of the class (all others use underscores, e.g. agent_handoff) and isn't a defined gen_ai.operation.name value in the GenAI semconv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gen-ai Related to generative AI

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[OpenAI Agents] MCP List Tools spans show only as "unknown" with no data

3 participants